package com.ai.module.mj.domain.bo;

import eu.maxschuster.dataurl.DataUrlEncoding;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * @author: heJinQin
 * @create: 2024-03-02 15:13
 * @description:
 **/

public class MJDataUrlBuilder  implements Serializable {

        private static final long serialVersionUID = 1L;

        /**
         * Payload of this data url
         */
        private byte[] data;

        /**
         * MIME-Type of this data urls content
         */
        private String mimeType;

        /**
         * Encoding method
         */
        private DataUrlEncoding encoding;

        /**
         * Headers/parameters of this data url
         */
        private Map<String, String> headers;
 
        public MJDataUrl build() throws NullPointerException {
            if (data == null) {
                throw new NullPointerException("data is null!");
            } else if (encoding == null) {
                throw new NullPointerException("encoding is null!");
            }
            return new MJDataUrl(data, encoding, mimeType, headers);
        }

        /**
         * Gets the payload of the data url
         * @return Payload of the data url
         */
        public byte[] getData() {
            return data;
        }

        /**
         * Sets the payload of the data url
         * @param data Payload of the data url
         * @return This {@link MJDataUrlBuilder} instance
         */
        public MJDataUrlBuilder setData(byte[] data) {
            this.data = data;
            return this;
        }

        /**
         * Gets the MIME-Type of the data urls content
         * @return MIME-Type of the data urls content
         */
        public String getMimeType() {
            return mimeType;
        }

        /**
         * Sets the MIME-Type of the data urls content
         * @param mimeType MIME-Type of the data urls content
         * @return This {@link MJDataUrlBuilder} instance
         */
        public MJDataUrlBuilder setMimeType(String mimeType) {
            this.mimeType = mimeType;
            return this;
        }

        /**
         * Sets the encoding method
         * @return Encoding method
         */
        public DataUrlEncoding getEncoding() {
            return encoding;
        }

        /**
         * Gets the encoding method
         * @param encoding Encoding method
         * @return This {@link MJDataUrlBuilder} instance
         */
        public MJDataUrlBuilder setEncoding(DataUrlEncoding encoding) {
            this.encoding = encoding;
            return this;
        }

        /**
         * Get the headers/parameters of the data url
         * @return Headers/parameters of the data url
         */
        public Map<String, String> getHeaders() {
            return headers;
        }

        /**
         * Set the headers/parameters of the data url
         * @param headers Headers/parameters of the data url
         * @return This {@link MJDataUrlBuilder} instance
         */
        public MJDataUrlBuilder setHeaders(Map<String, String> headers) {
            this.headers = headers;
            return this;
        }

        /**
         * Shorthand to set a header/parameter
         * @param name Name of the header/parameter
         * @param value Value of the header/parameter
         * @return This {@link MJDataUrlBuilder} instance
         */
        public MJDataUrlBuilder setHeader(String name, String value) {
            if (headers == null) {
                headers = new HashMap<>();
            }
            headers.put(name, value);
            return this;
        }

        /**
         * Shorthand to set the charset header
         * @param charset Charset name
         * @return This {@link MJDataUrlBuilder} instance
         */
        public MJDataUrlBuilder setCharset(String charset) {
            setHeader("charset", charset);
            return this;
        }

        public MJDataUrlBuilder setDataUrl(MJDataUrl template) {
            setData(template.getData());
            setEncoding(template.getEncoding());
            setHeaders(template.getHeaders());
            setMimeType(template.getMimeType());
            return this;
        }
    }
